package com.hb.medical_research_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.hb.medical_research_system.common.JWTUtils;

import com.hb.medical_research_system.common.Results;
import com.hb.medical_research_system.entity.User;
import com.hb.medical_research_system.entity.dto.UserDTO;
import com.hb.medical_research_system.mapper.UserMapper;
import com.hb.medical_research_system.service.LoginService;
import com.hb.medical_research_system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService{

    //TODO 1001为管理员, 1000为用户

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @Override
    public Results login(UserDTO user) {
        log.info("进入登录流程");

        if(StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
            return Results.info(Results.ERROR, "账户名或密码为空,登录失败");
        }

        // 密码md5处理
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //查询记录
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User realuser = userService.getOne(queryWrapper);

        //无查询结果,登录失败
        if(realuser == null){
            return Results.info(Results.ERROR,"用户不存在");
        }

        //密码错误,登陆失败
        if(!realuser.getPassword().equals(password)){
            return Results.info(Results.ERROR,"密码错误,登录失败");
        }

        log.info("登录成功,生成token");
        String token = JWTUtils.getJwtToken(realuser.getUsername(), realuser.getUserId());

        Results results = realuser.getFlag() == 1 ? Results.data(Results.ADMINE,token,"管理员登录成功"): Results.data(Results.USER,token,"用户登录成功");

        return results;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public Results register( UserDTO user) {
        log.info("开始注册新用户1");

        if(StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
            return Results.info(Results.ERROR,"账户名或密码为空,注册失败");
        }

        //查询用户
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        User one = userMapper.selectOne(lambdaQueryWrapper);

        //用户存在,注册失败
        if(one != null)
            return Results.info(Results.ERROR,"该用户已存在");

        User realuser = new User();
        realuser.setUsername(user.getUsername());
        //设置初始密码,MD5处理
        realuser.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));

        //设置创建及更新时间
        realuser.setCreateTime(LocalDateTime.now());
        realuser.setUpdateTime(LocalDateTime.now());
        //存入数据库
        userMapper.insert(realuser);

        return Results.info(Results.SUCCESS,"用户注册成功");
    }

    /**
     * 注册并登录
     * @param user
     * @return
     */
    @Override
    public Results register2(UserDTO user) {
        log.info("开始注册新用户2");

        if(StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
            return Results.info(Results.ERROR,"账户名或密码为空,注册失败");
        }

        //查询用户
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        User one = userMapper.selectOne(lambdaQueryWrapper);

        //用户存在,注册失败
        if(one != null)
            return Results.info(Results.ERROR,"该用户已存在");

        //设置初始密码,MD5处理
        User realuser = new User();
        realuser.setUsername(user.getUsername());
        //设置初始密码,MD5处理
        realuser.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));

        //设置创建及更新时间
        realuser.setCreateTime(LocalDateTime.now());
        realuser.setUpdateTime(LocalDateTime.now());
        //存入数据库
        userMapper.insert(realuser);

        String token = JWTUtils.getJwtToken(realuser.getUsername(), realuser.getUserId());
        log.info("注册成功,生成token");

        return Results.data(Results.USER,token,"用户登录成功");
    }


}
